﻿define(["../common"], function() {
	var pd = window.parent.document,
	temp = $("<div>").append($("table tr:gt(1)")).html(),
	version = null,
	setting = null,
	serialCode = null;
	
	window.setSetting = function(set) {
		setting = set;
	};
	
	window.initPrintQrcode = function(serial) {
		serialCode = serial;
		// 不打印二维码
		if (!setting.print.isPrintReceiptQrcode) {
			return;
		}
		
		var qrcodeType = parseInt(setting.print.receiptQrcodeType);
		var qrcodeContent = "";
		var storeId = $("#tstoreId").html();
		
		// 店铺地址
		if (qrcodeType === 0) {
			qrcodeContent = g_mobDomain + "/store/" + storeId;
			
		} // 记账地址
		else if (qrcodeType === 1) {
			qrcodeContent = g_mobDomain + "/user/memberSpend/addDo?storeId=" + storeId + "&serialCode=" + serialCode
			
		} // 自定义内容
		else if (qrcodeType === 2) {
			qrcodeContent = setting.print.receiptQrcodeContent;
			
		} else {
			log("小票二维码类型参数无效: setting.print.receiptQrcodeType ->" + qrcodeType);
			qrcodeContent = g_mobDomain + "/store/" + storeId;
		}
		$("#footText").html(setting.print.receiptFootText);
		$("[id^='qrcode_']:hidden").empty();
		require(["qrcode"], function() {
			$("[id^='qrcode_']:hidden").qrcode({size:100,text: qrcodeContent});
		});
	}
	
	window.initPrintGoods = function () {
		
		$("#serialCode").html(("" + serialCode).substr(0, serialCode.length - 10));// 部分
		$("#dateTime").html(new Date().format("yyyy-MM-dd hh:mm"));
		$("table tr:gt(1)").remove();
		$("[id^='goods_']", pd).each(function() {
			// storeGoodsId
			var vars = $(this).attr("id").split(/_/);
			var id = vars[1] + "_" + vars[2];
			var item = temp
			.replace(/\-\{name\}\-/g, $("#tname_" + id, pd).html())
			.replace(/\-\{price\}\-/g, $("#tprice_" + id, pd).html())
			.replace(/\-\{amount\}\-/g, $("#amount_" + id, pd).val())
			.replace(/\-\{cost\}\-/g, $("#cost_" + id, pd).val());
			$("table").append(item);
		});
		
		var total = parseFloat($("#totalMoney", pd).html()),
		shouldPay = parseFloat($("#pay4Settle", pd).html()),
		pay = parseFloat($("#pay", pd).val()),
		change = pay - shouldPay,
		back = 0;
		if (shouldPay < total) {
			back = total - shouldPay;
			$("#total,#back").parent("li").show();
		} else {
			$("#total,#back").parent("li").hide();
		}
		$("#num").html($("[id^='goods_']", pd).length);
		$("#total").html(total.toFixed(2));
		$("#back").html(back.toFixed(2));
		$("#shouldPay").html(shouldPay.toFixed(2));
		$("#pay").html(pay.toFixed(2));
		$("#change").html(change.toFixed(2));
		$("[id^='qrcode_']").toggle();
		
	};
});
